package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.ExtentDao;
import cn.topevery.garden.entity.po.Extent;
import cn.topevery.garden.entity.query.ExtentQuery;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.DbScriptHelper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author code-generate
 * @date 2021-08-06
 */
@Repository
public class ExtentDaoImpl extends BaseDaoImpl<Extent> implements ExtentDao {
    @Override
    public <P extends DbCsp> List<Extent> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<Extent> listPage(ExtentQuery query) {
        String table = "t_extent te";

        String columns = "te.c_id,te.c_park_id,te.c_park_name,te.c_code,te.c_name,te.c_type_id,te.c_type_name," +
                "te.c_address,te.c_area,te.c_link_user_id,te.c_link_user_name,te.c_link_phone,te.c_remark,te.c_entity_id,te.c_geom_bd09";
		int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("te.c_db_create_date desc");
        sqlQueryMate
                .where(true, "te.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(query.getParkId() != null, "te.c_park_id", ScriptConditionEnum.Eq, query.getParkId())
                .where(query.getType() != null, "te.c_type_id", ScriptConditionEnum.Eq, query.getType())
                .where(StringUtils.isNotEmpty(query.getName()), "te.c_name", ScriptConditionEnum.Like, query.getName())
                .where(StringUtils.isNotEmpty(query.getCode()), "te.c_code", ScriptConditionEnum.Like, query.getCode())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, Extent.class);
    }

    /**
     * 描述 通过公园Id和名称获取片区ID
     *
     * @param parkId the park id
     * @param name the name
     * @return the park id by code and name
     * @author wenzhao.lin
     * @created 2021 -08-19 14:44:45
     */
    @Override
    public Integer getExtentIdByParkIdAndName(Integer parkId, String name) {
        String table = "t_extent tp";
        String columns = "tp.c_id";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tp.c_park_id", ScriptConditionEnum.Eq, parkId)
                .where(StringUtils.isNotEmpty(name), "tp.c_name", ScriptConditionEnum.Eq, name)
        ;
        Integer tempId = null;
        List<Extent> list = selectList(sqlQueryMate, Extent.class);
        if(list != null && list.size() > 0){
            tempId = list.get(0).getId();
        }
        return tempId;
    }

    /**
     * 描述 检查是否已经存在.
     *
     * @param fieldName 字段名
     * @param value     字段值
     * @return the boolean （true已存在，false不存在）
     * @author wenzhao.lin
     * @created 2021 -08-19 09:23:47 Is exit boolean.
     */
    @Override
    public boolean isExit(String fieldName, String value) {
        String table = "t_extent";
        String columns = fieldName;
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, fieldName, ScriptConditionEnum.Eq, value)
        ;
        List<Extent> list = selectList(sqlQueryMate,Extent.class);
        if(list != null && list.size()>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<Map<String, Object>> options(ExtentQuery query) {
        String table = "t_extent te";
        List<String> columns = Arrays.asList("c_id as id", "c_name as name", "c_type_id as type", "c_geom_bd09 as geom");

        SqlQueryMate sqlQueryMate = DbScriptHelper.getSelectSqlMate(table).setColumns(Strings.join(columns, ','));
        sqlQueryMate
                .where(true, "te.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getType()) && StringUtils.isNotEmpty(query.getType()), "te.c_type_id", ScriptConditionEnum.Eq, query.getType());
        ;
        return getMap(sqlQueryMate);
    }

    @Override
    public List<Extent> getExtentsByLngLat(double lng,double lat, Integer distance){
        String table = "t_extent te";
        String columns = "te.c_id,te.c_park_id,te.c_park_name,te.c_code,te.c_name,te.c_type_id,te.c_type_name," +
                "te.c_address,te.c_area,te.c_link_user_id,te.c_link_user_name,te.c_link_phone,te.c_remark,te.c_entity_id,te.c_geom_bd09";

        SqlQueryMate sqlQueryMate = DbScriptHelper.getSelectSqlMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "te.c_db_status", ScriptConditionEnum.Eq, 0)
                .where( true, "st_distance(POINT("+lng+","+lat+"), te.c_geom_bd09)", ScriptConditionEnum.LessThanEq, distance)
        ;
        return selectList(sqlQueryMate,Extent.class);
    }
}
